package com.xzg.erp.system.web;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.xzg.erp.common.Constants;
import com.xzg.erp.system.entity.SysRole;
import com.xzg.erp.system.entity.dto.RolePageDto;
import com.xzg.erp.system.entity.dto.RoleQueryDto;
import com.xzg.erp.system.service.ISysRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Objects;
/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author qwc
 * @since 2025-05-12
 */
@RestController
@RequestMapping("/sysRole")
public class SysRoleController {

    @Autowired
    private ISysRoleService roleService;

    @Autowired
    private ObjectMapper objectMapper;

    @PostMapping("/page")
    public ResponseEntity page(@RequestBody RoleQueryDto dto) {
        Page<SysRole> page = new Page<>(dto.getPageNo(), dto.getPageSize());
        Page<SysRole> page1 = roleService.page(page, Wrappers.<SysRole>lambdaQuery()
                .like(Objects.nonNull(dto.getId()), SysRole::getId, dto.getId())
                .like(StrUtil.isNotBlank(dto.getName()), SysRole::getName, dto.getName())
                .eq(SysRole::getDeleted, Constants.UN_DELETED)
                .orderByDesc(SysRole::getUpdatedAt));
        IPage<RolePageDto> convert = page1.convert(role -> objectMapper.convertValue(role, RolePageDto.class));
        return ResponseEntity.ok(convert);
    }

}
